﻿@{
    ViewBag.Title = "入库";
    var proList = ViewBag.ProList as List<CubicFWNet.Data.Models.Product>;
    var orderList = ViewBag.OrderList as List<CubicFWNet.Data.ViewModal.OrderInfo>;
    var product = ViewBag.Product as CubicFWNet.Data.Models.Product;
}

<section class="content-header">
    <h1>
        入库
    </h1>
    <ol class="breadcrumb">
        <li><a href="javascript:void(0);"><i class="fa fa-dashboard"></i>@Resources.Global.PageTitle</a></li>
        <li><a href="@Url.Action("Index","Home")">首页</a></li>
        <li class="active">入库</li>
    </ol>
</section>

<section class="content">
    <form class="form-horizontal" method="post" id="formMain">
        <div class="box box-solid">
            <div class="box-body">
                <div class="row">
                    <input type="hidden" id="PID" name="PID" value="@product.Id" />
                    <div class="form-group col-md-6 col-sm-12">
                        <label for="ProductName" class="col-sm-3 control-label">选择产品</label>
                        <div class="col-sm-9">
                            <span onclick="ShowProList()" class="btn btn-primary ">选择产品</span>
                        </div>
                    </div>
                    <div class="form-group col-md-6 col-sm-12">
                        <label for="ProName" class="col-sm-3 control-label">产品信息</label>
                        <div class="col-sm-9">
                            <span onclick="ShowModal('@product.Id')" name="ProName" id="ProName" class="form-control linkLayout">@product.Name</span>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="form-group col-md-6 col-sm-12">
                        <label for="OrderInfo" class="col-sm-3 control-label">订单信息</label>
                        <div class="col-sm-9">
                            <span onclick="" name="OrderInfo" id="OrderInfo" class="form-control linkLayout"></span>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="form-group col-md-6 col-sm-12">
                        <label for="Amount" class="col-sm-3 control-label">数量</label>
                        <div class="col-sm-9">
                            <input type="number" class="form-control" id="Amount" name="Amount" maxlength="20" min="0"/>
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="form-group col-md-6 col-sm-12">
                        <label for="Memo" class="col-sm-3 control-label">备注</label>
                        <div class="col-sm-9">
                            <textarea rows="3" class="form-control" id="Memo" name="Memo" maxlength="500"></textarea>
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="form-group col-md-6 col-sm-12">
                        <label for="Creator" class="col-sm-3 control-label">创建人</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="Creator" name="Tolerance" value="@ViewBag.RealName" readonly="readonly" />
                        </div>
                    </div>
                    <div class="form-group col-md-6 col-sm-12">
                        <label for="CreateTime" class="col-sm-3 control-label">创建时间</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="CreateTime" value="@DateTime.Now.ToString("yyyy-MM-dd HH:mm")" readonly="readonly" />
                        </div>
                    </div>
                </div>
            </div>

            <div class="box-footer">
                <span class="btn btn-primary " onclick="FormSub()">确 定</span>&nbsp;&nbsp;&nbsp;&nbsp;
            </div>

        </div>
    </form>
</section>

<!--选择产品-->
<div class="modal" id="proList" style="display: none">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">选择产品</h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="form-group  col-sm-12">
                        <label for="PName" class="col-sm-3 control-label" onclick="GetProduct()"><span style="color:dodgerblue">搜索产品</span></label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="PName" name="PName" placeholder="请输入产品名称..." onkeyup="GetProduct()" />
                        </div>
                    </div>
                </div>
                <div id="cusProList">
                    @if (proList != null)
                    {
                        foreach (var item in proList)
                        {
                            <span class="btn btn-default productItem" data-proID="@item.Id" data-sel="0" style="margin:8px">@item.Name</span>
                        }
                    }
                </div>
            </div>
            <div class="modal-footer">
                <a role="button" class="btn btn-default pull-left" data-dismiss="modal">关闭</a>
                <a role="button" class="btn btn-primary" data-dismiss="modal">完成</a>
            </div>
        </div>
    </div>
</div>

@section Scripts {
    <script>
        $(document).ready(function () {
            //sunjian 选择默认订单为：公共库存
            $("#selOrder").val(0);
        })

        //提交表单
        function FormSub() {
            var prpName = $("#ProName").html().trim();
            if (prpName == "" || prpName == null) {
                $.modalAlert("请选择产品", "warning");
                return false;
            }

            var orderInfo = $("#OrderInfo").html().trim();
            if (orderInfo == "" || orderInfo == null) {
                $.modalAlert("请选择订单", "warning");
                return false;
            }

            var amount = $("#Amount").val().trim();
            if (amount == "" || amount == null) {
                $.modalAlert("数量有误！", "warning");
                return false;
            }
            if (amount < 0) {
                $.modalAlert("数量有误！", "warning");
                return false;
            }

            $("#formMain").submit();
        }

        //选择产品弹窗
        function ShowProList() {
            $("#proList").modal();
        }

        function ShowOrderList() {
            $("#orderList").modal();
        }

        //弹窗显示产品信息
        function ShowModal(id) {

                var options = {
                    id: null,
                    title: '产品信息',
                    width: "90%",
                    height: "90%",
                    url: "../Product/Info?PID=" + id,
                    shade: 0.3,
                    btn: ['返回'],
                    btnclass: ['btn btn-danger'],
                    callBack: null
                }
                $.modalOpen(options);
            
        }

        //弹窗显示订单信息
        function ShowOrderModal(id) {
            if (id != '00000000-0000-0000-0000-000000000000') {
                var options = {
                    id: null,
                    title: '订单信息',
                    width: "90%",
                    height: "90%",
                    url: "../Order/OrderInfo?OID=" + id,
                    shade: 0.3,
                    btn: ['返回'],
                    btnclass: ['btn btn-danger'],
                    callBack: null
                }
                $.modalOpen(options);
            }
        }

        //选择产品事件
        $(".productItem").click(function () {
            var id = $(this).attr("data-proID");
            location.href = "PutInProduct?PID=" + id;
        });

        //选择订单事件
        $("#selOrder").change(function () {
            var id = $(this).children('option:selected').attr("data-id");
            var number = $(this).children('option:selected').val();
            var uid = $(this).children('option:selected').attr("data-uid");
            $("#OrderInfo").attr("onclick", "ShowOrderModal('" + id + "')");
            //sunjian 如果number为0，则【订单信息】显示 空，否则 显示【number】
            $("#OrderInfo").html(number == "0" ? "" : number);  
            $("#OID").val(id);
            $("#UID").val(uid);
        })

        //搜索产品
        function GetProduct() {
            var name = $("#PName").val().trim();

            var proList = $("#cusProList .productItem");
            var num = proList.size();

            for (var i = 0; i < num; i++) {
                var inName = $("#cusProList .productItem").eq(i).html();
                if (inName.indexOf(name) >= 0) {
                    $("#cusProList .productItem").eq(i).show();
                }
                else {
                    $("#cusProList .productItem").eq(i).hide();
                }
            }
        }
    </script>
}